﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class NewFile : Form
    {
        public string Filename
        {
            get
            {
                return textBox1.Text;
            }
        }

        private Project project;

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) Close();

            return base.ProcessCmdKey(ref msg, keyData);
        }

        public NewFile(Project p)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            button1.Enabled = false;
            project = p;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (listView1.SelectedItems[0].ImageIndex == 0)
                    textBox1.Text = "Header1.h";
                if (listView1.SelectedItems[0].ImageIndex == 1)
                    textBox1.Text = "Source1.c";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(project.HasFilename(textBox1.Text))
            {
                MessageBox.Show(this, "Specified filename already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
        }

        private void NewFile_Load(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}
